package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocMonthAddDTO;
import org.jsola.hr.dto.EmpSocMonthUpdateDTO;
import org.jsola.hr.query.EmpSocMonthQuery;
import org.jsola.hr.query.SocCheckCustomQuery;
import org.jsola.hr.service.IEmpSocMonthService;
import org.jsola.hr.vo.EmpSocMonthListVO;
import org.jsola.hr.vo.EmpSocMonthVO;
import org.jsola.hr.vo.MonthNextAndPreviousCheckVO;
import org.jsola.hr.vo.YearAndMonthVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 员工参保信息月统计
 *
 * @author zhr
 */
@RestController("hrEmpSocMonthController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocMonths")
public class EmpSocMonthController implements IEmpSocMonthControllerAPI {

    @Autowired
    private IEmpSocMonthService empSocMonthService;

    @Override
    @PostMapping
    public Result<EmpSocMonthVO> save(@Valid @RequestBody EmpSocMonthAddDTO empSocMonthAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息月统计权限校验

        return Result.success(
                empSocMonthService.save(empSocMonthAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/initSave")
    public Result<EmpSocMonthVO> initSave(@Valid @RequestBody EmpSocMonthAddDTO empSocMonthAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息月统计权限校验

        return Result.success(
                empSocMonthService.initSave(empSocMonthAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocMonthUpdateDTO empSocMonthUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工参保信息月统计权限校验

        int count = empSocMonthService.update(empSocMonthUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empSocMonthId}")
    public Result<Integer> delete(@PathVariable String empSocMonthId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工参保信息月统计权限校验

        int count = empSocMonthService.deleteByIds(tokenUser, empSocMonthId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/deleteByCompanyIdAndYearMonth")
    public Result<Integer> deleteByCompanyIdAndYearMonth(@RequestParam(value = "companyId") String companyId,
                                                         @RequestParam(value = "year") String year,
                                                         @RequestParam(value = "month") String month) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 删除员工参保信息月统计权限校验

        int count = empSocMonthService.deleteByCompanyIdAndYearMonth(companyId, year, month, tokenUser);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocMonthIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工参保信息月统计权限校验

        int count = empSocMonthService.deleteByIds(tokenUser, empSocMonthIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocMonthId}")
    public Result<EmpSocMonthVO> findById(@PathVariable String empSocMonthId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息月统计权限校验

        return Result.success(
            empSocMonthService.selectById(empSocMonthId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocMonthListVO>> listAll(@Valid EmpSocMonthQuery empSocMonthQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息月统计权限校验

        return Result.success(
            empSocMonthService.select(empSocMonthQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocMonthListVO>> page(@Valid EmpSocMonthQuery empSocMonthQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empSocMonthService.selectPage(empSocMonthQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/listYearAndMonth/{companyId}")
    public Result<List<YearAndMonthVO>> listYearAndMonthByCompany(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息表权限校验

        return Result.success(
                empSocMonthService.listYearAndMonthByCompany(companyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/checkMonthNextAndPrevious")
    public Result<MonthNextAndPreviousCheckVO> checkMonthNextAndPrevious(@RequestParam("year") String year,
                                                                         @RequestParam("month") String month,
                                                                         @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保月表权限校验

        return Result.success(
                empSocMonthService.checkMonthNextAndPrevious(year, month, companyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAllByCompanyId")
    public Result<List<EmpSocMonthListVO>> listAllByCompanyId(@Valid SocCheckCustomQuery socCheckCustomQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保月表权限校验

        return Result.success(
                empSocMonthService.listAllByCompanyId(socCheckCustomQuery, tokenUser)
        );
    }

}

